# -*- coding: utf-8 -*-
# @Time : 2024/2/26 9:38
# @Author : ZH
# @File : 938.py
# @Software: PyCharm

class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def rangeSumBST(self, root, low, high):
        """
        :type root: TreeNode
        :type low: int
        :type high: int
        :rtype: int
        """
        raw = []

        def rrr(node):
            if not node:
                return
            if low <= node.val <= high:
                raw.append(node.val)
                rrr(node.left)
                rrr(node.right)
            elif node.val <= low:
                rrr(node.right)
            elif node.val >= high:
                rrr(node.left)

        rrr(root)
        # print(raw)
        return sum(raw)

